package com.ktjy.ch15.controller;

import com.ktjy.ch15.entity.Computerroom;
import com.ktjy.ch15.entity.SysUser;
import com.ktjy.ch15.service.ComputerroomService;
import com.ktjy.ch15.utils.Constants;
import com.ktjy.ch15.utils.Page;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main/computerroom")
public class ComputerroomController extends BaseController{
    private static final Logger LOGGER = LoggerFactory.getLogger(ComputerroomController.class);

    @Autowired
    private ComputerroomService computerroomService;

    @RequestMapping("/page")
    public String page(Model model, Computerroom condition,
                       @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        Page<Computerroom> page = null;
        try {
            // 查询用户的分页信息
            page = computerroomService.findPage(condition, pageNo, pageSize);
            // 将分页数据、角色列表和查询条件设置到模型数据中
            model.addAttribute("pageInfo", page);
            model.addAttribute("condition", condition);
        } catch (Exception e) {
            LOGGER.error("ComputerroomController 中的 page 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "computerroom/page";
    }

    @GetMapping("/save")
    public String toSave(Model model) {
        try {
            // 回显数据时需要 sysUser，这里给个空对象避免出错
            model.addAttribute("computerroom", new Computerroom());
        } catch (Exception e) {
            LOGGER.error("SysUserController中的toSave方法执行发生错误。", e);
            throw new RuntimeException(e);
        }
        return "computerroom/save";
    }

    @PostMapping("/save")
    public String doSave(Computerroom computerroom, HttpSession session, HttpServletRequest request) {
        try {
            boolean ok =false;
            if(computerroom.getRoomId()==null){//新增
                ok = computerroomService.save(computerroom);
            }else{//修改
                ok= computerroomService.update(computerroom);
            }

            if(ok){
                //跳转到列表;重定向到分页方法
                return redirect("/main/computerroom/page");
            }else{
                //跳回新增页面
                return "computerroom/save";
            }
        } catch (Exception e) {
            LOGGER.error("SysUserController 中的 save 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/view/{roomId}")
    public String view(@PathVariable("roomId") Long roomId, Model model){
        Computerroom computerroom = null;
        try {
            computerroom = computerroomService.findByRoomId(roomId);
            model.addAttribute("computerroom", computerroom);
        } catch (Exception e) {
            LOGGER.error("SysUserController 中的 view 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "computerroom/view";
    }

    @GetMapping("/edit")
    public String toEdit(@RequestParam("roomId") Long roomId, Model model) {
        Computerroom computerroom = null;
        try {
            computerroom = computerroomService.findByRoomId(roomId);
            // 回显数据时需要 sysUser，这里给个空对象避免出错
            model.addAttribute("computerroom", computerroom);
        } catch (Exception e) {
            LOGGER.error("SysUserController 中的 toEdit 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "computerroom/save";
    }

    @DeleteMapping("/del/{roomId}")
    @ResponseBody
    public Object del(@PathVariable("roomId") Long roomId) {
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            boolean ok = computerroomService.delete(roomId);
            if (ok){
                map.put("code",200);
                map.put("msg","删除成功");
            }else {
                map.put("code",500);
                map.put("msg","删除失败");
            }
        }catch (Exception e){
            LOGGER.info("发生错误",e);
            map.put("code",500);
            map.put("msg","发生异常");
        }
        return map;
    }

    @GetMapping("/accountExist")
    @ResponseBody
    public Object accountExist(@RequestParam("roomName") String roomName) {
        Map<String,Object> map = new HashMap<>();
        try {
            Computerroom computerroom = computerroomService.findByRoomName(roomName);
            if (computerroom != null){
                map.put("exist",1);
            }else {
                map.put("exist",0);
            }
        }catch (Exception e){
            LOGGER.error("SysUserController中的accountExists方法执行发生错误", e);
            map.put("exist",-1);
            map.put("error", "出现异常，请联系管理员处理！ ");
        }
        return map;
    }

}
